# 导入必要的库
from sklearn.cluster import KMeans
import pandas as pd
import numpy as np

# 加载订单数据集
df = pd.read_excel('./input/meal_order_detail.xlsx')

# 根据下单时间，转换为午餐/晚餐信息
# 午餐为0，晚餐为1
def get_meal_type(place_order_time):
    hour = place_order_time.hour
    return 0 if place_order_time < 16 else 1

# 提取下单时间特征
hour = pd.to_datetime(df['place_order_time'])

# 标准化特征（可选）
# from sklearn.preprocessing import StandardScaler
# scaler = StandardScaler()
# scaled_features = scaler.fit_transform(features)

# 启用 KMeans 算法进行聚类
kmeans = KMeans(n_clusters=2, random_state=0, n_init=10).fit(hour.values.reshape(-1, 1))

def recommend_similar_orders(user_order_time, n_recommendations=5):
    user_order_time = np.array(user_order_time)  # 将列表转换为数组
    user_cluster = kmeans.predict(user_order_time.reshape(1, -1))[0]  #修改使用1和0表示的用户订单时间 
    similar_orders = df[kmeans.labels_ == user_cluster]
    recommendations = similar_orders.sample(n=n_recommendations)
    return recommendations


# 示例使用
user_order_time = [0]  # 用户的订单特征，根据实际数据进行设置
recommendations = recommend_similar_orders(user_order_time, n_recommendations=5)
print(recommendations)